package com.project.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("scope")
public class ScopeController {
    @RequestMapping("bind")
    public String  bindData(HttpServletRequest request){
        //在请求范围内共享数据
        request.setAttribute("requestScope","request Data");
        //得到会话对象
        HttpSession session = request.getSession();
        //在同一个用户会话范围内共享数据
        session.setAttribute("sessionScope","session data");
        //得到上下文对象
        ServletContext context = request.getServletContext();
        //在整个WEB应用程序中共享数据
        context.setAttribute("contextScope","context data");
        return "/scope/getData";
    }

    @RequestMapping("getData")
    @ResponseBody
    public String getData(HttpServletRequest request){
        String str = "";
        str += "请求范围："+request.getAttribute("requestScope")+"<br>";
        str += "会话范围："+request.getSession().getAttribute("sessionScope")+"<br>";
        str += "全局范围："+request.getServletContext().getAttribute("contextScope")+"<br>";

//        request.getSession().invalidate();
        return str;
    }


}
